package com.zllkj.mm.module.home.fragments

import android.view.View
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.zllkj.mm.R
import com.zllkj.mm.adapter.FragmentBaseAdapter
import com.zllkj.mm.base.BaseTitleFragment
import com.zllkj.mm.module.moment.FriendListFragment
import kotlinx.android.synthetic.main.fragment_square.view.*

/**
 * 广场
 */
class SquareFragment(val dataType:Int) : BaseTitleFragment() {

    companion object{
        var FRIEND_DATA_TYPE_DEFAULT = 0
        var FRIEND_DATA_TYPE_ONLY_FREE_TIME = 1
    }


    private val titles = arrayOf("附近", "关注")
    private val nearByListItemFragments = arrayListOf<FriendListFragment>()


    override fun getContentLayoutId(): Int {
        return R.layout.fragment_square
    }

    override fun initView(rootView: View) {
        super.initView(rootView)
        setCommonTitle("广场")
        hideTitle()


        nearByListItemFragments.add(FriendListFragment(0,dataType))
        nearByListItemFragments.add(FriendListFragment(1,dataType))


        mRootView?.findViewById<TabLayout>(R.id.tab_square_tab)?.let { tablayout ->
            titles.mapIndexed { index, s -> mRootView?.tab_square_tab?.addTab(tablayout.newTab().setText(s))}
        }

        mRootView?.tab_square_tab_viewpager?.apply {
            adapter= FragmentBaseAdapter(childFragmentManager, nearByListItemFragments, titles)
            addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
                override fun onPageSelected(position: Int) {
                    mRootView?.tab_square_tab?.let {
                        it.setScrollPosition(position,0f,false)
                    }
                }
            })
        }
        mRootView?.tab_square_tab_viewpager?.currentItem = 0

        mRootView?.tab_square_tab?.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                mRootView?.tab_square_tab_viewpager?.currentItem = tab.position
            }
            override fun onTabUnselected(tab: TabLayout.Tab) {}
            override fun onTabReselected(tab: TabLayout.Tab) {}
        })


    }

}